/**
 * 
 */
package client;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;

import server.ServerInterface;
import server.TweetStack;

/**
 * @author Machado
 *
 */
public class ClientMainServer {
	
	static private String localhost;
	static private String serverhost;
	static private String name;
	
	
	public static LinkedList<String> init(String[] args){
		
		
		LinkedList<String> tags = new LinkedList<String>();

		int range = args.length;
		for (int i=0; i<range;i++){
			System.out.println(args[i]);
			if ((args[i]).equals("-s")){
				serverhost = args[i+1];
				i++;
			}
			if ((args[i]).equals("-l")){
				localhost = args[i+1];
				i++;
			}
			if ((args[i]).equals("-n")){
				name = args[i+1];
				i++;
			}
			if ((args[i]).equals("-t")){
				tags.add(args[i+1]);
				i++;
			}
		}

		if (name == null){
			System.out.println("Not enough arguments");
			System.out.println("Usage: -n 'name' -l'local_ip' -s 'server_ip' -t 'tag' ");
			System.exit(0);
		}
		if (localhost == null){
			System.out.println("Not enough arguments");
			System.out.println("Usage: -n 'name' -l'local_ip' -s 'server_ip' -t 'tag' ");
			System.exit(0);
		}
		if (serverhost == null){
			System.out.println("Not enough arguments");
			System.out.println("Usage: -n 'name' -l'local_ip' -s 'server_ip' -t 'tag' ");
			System.exit(0);
		}
		if (tags.size()>3){
			System.out.println("Too many tags, 3 maximum");
			System.out.println("Usage: -n 'name' -l'local_ip' -s 'server_ip' -t 'tag' ");
			System.exit(0);
		}
		if (tags.size()==0){
			System.out.println("Not enough arguments");
			System.out.println("Usage: -n 'name' -l'local_ip' -s 'server_ip' -t 'tag' ");
			System.exit(0);
		}
		return tags;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		LinkedList<String> tags = init(args);
		
		TweetStack stack = new TweetStack();		
		ClientServer client = new ClientServer(stack);
		new Thread(client).start();

		try {
			java.rmi.registry.LocateRegistry.createRegistry(1099);
			System.out.println("RMI registry ready.");
		} catch (Exception e) {
			System.out.println("Exception starting RMI registry:");
			e.printStackTrace();
		}

		try {
			ClientInterface stub = (ClientInterface) UnicastRemoteObject.exportObject(client, 0);
			Registry registry = LocateRegistry.getRegistry();
			registry.rebind(name, stub);
			System.out.println("ClientServer bound");
		} catch (Exception e) {
			System.err.println("ClientServer exception:");
			e.printStackTrace();
		}

		int tries = 0;

		while(tries<3)	{
			try {
				Registry registry = LocateRegistry.getRegistry(serverhost);
				ServerInterface stub = (ServerInterface) registry.lookup("ClientManager");
				stub.Connect(localhost, tags, name);
				System.out.println("Connected: ");
				break;
			} catch (Exception e) {
				System.out.println("Try "+tries+" failed ...");
				System.err.println("Client exception: " + e.toString());
				e.printStackTrace();
				tries++;
			}
		}     

		if (tries == 3){
			System.out.println("The tweet me too server must be offline");
			System.out.println("Restart the client server at a later time");
			System.exit(0);
		}

		tries = 0;

		while(true)	{
			try {
				System.out.println("Tweet me too server is responding.");
				Registry registry = LocateRegistry.getRegistry(serverhost);
				ServerInterface stub = (ServerInterface) registry.lookup("ClientManager");
				stub.isAlive();
				Thread.sleep(10000);
			} catch (Exception e) {
				System.out.println("Tweet me too server not responding.");
				System.err.println("Client exception: " + e.toString());
				e.printStackTrace();
				System.out.println("The tweet me too server must be offline");
				System.out.println("Start the client server at a later time");
				System.exit(0);
			}
		}

	}

}
